#ifndef AGGREGATOR_H
#define AGGREGATOR_H

#include <vector>
#include <mutex>
#include <string>
#include "ServerApp.h"
#include "ZKPManager.h"
#include "DualSignature.h"
#include "CredentialManager.h"
#include "BeaconManager.h"
using namespace std;

struct AuthRequest {
    string user_id;
    Credential cred;
    ZKPProof zkp_proof;
    Beacon beacon;
    TicketRequest ticket_req;
    pair<Signature, Signature> dual_sig;
    bool isLogin;
};

struct AuthResult {
    string user_id;
    bool success;
    string reason;
    TicketResponse ticket_resp;
};

class Aggregator {
private:
    vector<AuthRequest> requests;
    mutex mtx;

    ServerApp* server;
    ZKPManager* zkp_mgr;
    DualSignature* ds_mgr;
    CredentialManager* cred_mgr;
    BeaconManager* beacon_mgr;

public:
    Aggregator(ServerApp* srv, ZKPManager* zkp, DualSignature* ds,
               CredentialManager* cred, BeaconManager* beacon)
        : server(srv), zkp_mgr(zkp), ds_mgr(ds), cred_mgr(cred), beacon_mgr(beacon) {}

    void addRequest(const AuthRequest& req) {
        lock_guard<mutex> lock(mtx);
        requests.push_back(req);
    }

    vector<AuthResult> verifyBatch(PublicKey ca_pk) {
        vector<AuthResult> results;
        lock_guard<mutex> lock(mtx);
        for (auto& req : requests) {
            AuthResult res;
            res.user_id = req.user_id;
            res.success = true;
            results.push_back(res);
        }
        requests.clear();
        return results;
    }
};

#endif // AGGREGATOR_H
